package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.MediumMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Medium;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IMediumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MediumServiceImpl implements IMediumService {

    @Resource
    MediumMapper mapper;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public void deleteMedium(Integer mediumId) {

        Medium medium = mapper.selectById(mediumId);

        Wrapper<Medium> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + medium.getId() + "]%");
        List<Medium> subMediums = mapper.selectList(wrapper);
        for (Medium temp : subMediums) {
            temp.deleteById();
        }

        medium.deleteById();
    }

    public List<Medium> getThrMedium() {
        String sql="select * FROM medium a where length(a.pids)=14" +
                " and a.mediumname!='请选择'";
        List<Medium> mediumList =sqlMapper.selectList(sql,Medium.class);
        return mediumList;
    }


    public List<Map<String, Object>> getMedium() {
        String sql = "select id, pids,mediumname from medium order by  num asc;";
        List<Map<String, Object>> mediumList = sqlMapper.selectList(sql);
        List<Map<String, Object>> oneMediumList = new ArrayList<>();
        List<Map<String, Object>> twoMediumList = new ArrayList<>();
        List<Map<String, Object>> thrMediumList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < mediumList.size(); i++) {
            if (mediumList.get(i).get("pids").toString().split(",").length < 2) {
                oneMediumList.add(mediumList.get(i));
            } else if (mediumList.get(i).get("pids").toString().split(",").length == 2) {
                twoMediumList.add(mediumList.get(i));
            } else if (mediumList.get(i).get("pids").toString().split(",").length == 3) {
                thrMediumList.add(mediumList.get(i));
            }
        }
        List<Map<String, Object>> relist = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < oneMediumList.size(); i++) {
            Map<String, Object> oneMediumMap = new HashMap<String, Object>();
            List<Map<String, Object>> listmap2 = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < twoMediumList.size(); j++) {
                if (("[" + oneMediumList.get(i).get("id").toString() + "]").equals(twoMediumList.get(j).get("pids").toString().split(",")[1])) {
                    Map<String, Object> twomediumMap = new HashMap<String, Object>();
                    List list3 = new ArrayList<>();
                    for (int k = 0; k < thrMediumList.size(); k++) {
                        if (("[" + twoMediumList.get(j).get("id").toString() + "]").equals(thrMediumList.get(k).get("pids").toString().split(",")[2])) {
                            list3.add(thrMediumList.get(k).get("mediumname").toString());
                        }
                    }
                    twomediumMap.put("name", twoMediumList.get(j).get("mediumname").toString());
                    twomediumMap.put("areaList", list3);
                    listmap2.add(twomediumMap);
                    oneMediumMap.put("citiesList", listmap2);
                    oneMediumMap.put("name", oneMediumList.get(i).get("mediumname").toString());
                }else{
                    oneMediumMap.put("name", oneMediumList.get(i).get("mediumname").toString());
                    oneMediumMap.put("citiesList", listmap2);
                }
            }
            relist.add(oneMediumMap);
            if (relist.get(i).size() == 0) {
                relist.remove(relist.get(i));
            }
        }
        return relist;
    }
}
